<?php
/*
积分商城管理 ，发布宝贝 和 编辑 下架宝贝
*/
class CreditshopController extends MyClass_Action {
	protected $controller;
	protected $action;
	protected $_user;
	protected $prepage=10;
	
	public function init() {
		$this->commonAction();	
	}
	
	
	public function indexAction(){
		try {
/*			$sql="SELECT * FROM `setting`";
			$result=$this->_dbAdapter ->fetchAssoc ( $sql );
			//print_r($result);exit;
			$this->view->result=$result;
*/	
	} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}

	public function opsaveAction(){
		try {
			if (! $this->isPost ()) {
				throw new Exception ( 'parameter_error' );
			}
			$p =$this->_request->getPost ();
			if($p['name']==''){
				throw new Exception ( '姓名不能为空' );
			}
			if($p['content']=='' ){
				throw new Exception ( '内容不能为空' );
			}
			if($p['score']=='' ){
				throw new Exception ( '内容不能为空' );
			}

			if($p['score']<0 ){
				throw new Exception ( '积分不能小于0' );
			}
			
			$p['score']=(int) $p['score'];
			$p['name']=strip_tags($p['name']);	
			$p['name']=addslashes($p['name']);
			
			$p['content']=addslashes(strip_tags($p['content']));	
			
			//print_r($p);exit;
			
			$data=array(
				'goodsname'=>$p['name'],
				'credit'=>$p['score'],
				'addtime'=>time(),
				'intro'=>$p['content'],
			);
			$result = $this->_dbAdapter->insert ( 'credit_goods', $data );
			$goodsid = $this->_dbAdapter->lastInsertId (); //获得刚插入的活动id
			
		    if(isset($_FILES['pic_url']['tmp_name']) && !empty($_FILES['pic_url']['tmp_name']) && $goodsid){
				  
				  $file_path=PROJECT_ROOT .'/public/credit/';	
				  if (!file_exists($file_path)) {
					  mkdir($file_path);
				  }  
				  
				   $this->ThumbRatio($_FILES['pic_url']['tmp_name'],$file_path.$goodsid.'.jpg.small.jpg',100,100,4);
				   $this->ThumbRatio($_FILES['pic_url']['tmp_name'],$file_path.$goodsid.'.jpg',1136,640,1);
					  					
					$res1=Azure_Storage::defaultImg('credit',$file_path.$goodsid.'.jpg.small.jpg',$goodsid.'.small.jpg');
					$res2=Azure_Storage::defaultImg('credit',$file_path.$goodsid.'.jpg',$goodsid.'.jpg');//上传到云服务器上面 1、文件夹的名字；2、文件的绝对路径；3、图片名字
					  
				 
				  if($res1 && $res2){
					//删除本地图片
					unlink($file_path.$goodsid.'.small.jpg');
					unlink($file_path.$goodsid.'.jpg');
				  }else{
					throw new Exception ('失败');
				  }
				  
				  $data1['filename']=$goodsid;
				  $data1['filesize']=$_FILES['pic_url']['size'];
				  $data1['fext']='jpg';
				  $result = $this->_dbAdapter->update ( 'credit_goods', $data1,"goodsid='".$goodsid."'" );
	   		} 
			
			if ($result) {
				$this->feedback ( 'success', 'success', '/'.$this->controller.'/index' , 'tip', 3 );
			} else {
				throw new Exception ( '操作失败,或已经删除，请先刷新查看！' );//操作失败,或已经删除，请先刷新查看！
			}

		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}

	public function opeditAction(){
		try {
			if (! $this->isPost ()) {
				throw new Exception ( 'parameter_error' );
			}
			$p =$this->_request->getPost ();
			
			//print_r($p);exit;
			if($p['name']==''){
				throw new Exception ( '姓名不能为空' );
			}
			if($p['content']=='' ){
				throw new Exception ( '内容不能为空' );
			}
			if($p['score']=='' ){
				throw new Exception ( '内容不能为空' );
			}

			if($p['score']<0 ){
				throw new Exception ( '积分不能小于0' );
			}
			
			$p['score']=(int) $p['score'];
			$p['gid']=(int) $p['gid'];
			$p['name']=addslashes(strip_tags($p['name']));	
			$p['content']=addslashes(strip_tags($p['content']));	
			
			$data=array(
				'goodsname'=>$p['name'],
				'credit'=>$p['score'],
				'addtime'=>time(),
				'intro'=>$p['content'],
			);
			
		    if(isset($_FILES['pic_url']['tmp_name']) && !empty($_FILES['pic_url']['tmp_name'])){
				
				  $file_path=PROJECT_ROOT .'/public/credit/';	
				  if (!file_exists($file_path)) {
					  mkdir($file_path);
				  }  
				  
				   $this->ThumbRatio($_FILES['pic_url']['tmp_name'],$file_path.$p['gid'].'.jpg.small.jpg',100,100,4);
				   $this->ThumbRatio($_FILES['pic_url']['tmp_name'],$file_path.$p['gid'].'.jpg',1136,640,1);
					  					
					$res1=Azure_Storage::defaultImg('credit',$file_path.$p['gid'].'.jpg.small.jpg',$p['gid'].'.jpg.small.jpg');
					$res2=Azure_Storage::defaultImg('credit',$file_path.$p['gid'].'.jpg',$p['gid'].'.jpg');
					
				  if($res1 && $res2){
					//删除本地图片
					unlink($file_path.$p['gid'].'.small.jpg');
					unlink($file_path.$p['gid'].'.jpg');
				  }
				  
				  $data['filename']=$p['gid'];
				  $data['filesize']=$_FILES['pic_url']['size'];
				  $data['fext']='jpg';
	   		} 
			
			$result = $this->_dbAdapter->update ( 'credit_goods', $data ,"goodsid='".$p['gid']."'" );
			
			if ($result) {
				$this->feedback ( 'success', 'success', '/'.$this->controller.'/edit/gid/'.$p['gid'] , 'tip', 3 );
			} else {
				throw new Exception ( '操作失败,或已经删除，请先刷新查看！' );//操作失败,或已经删除，请先刷新查看！
			}

		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}


	public function publishedAction(){
		try {
				$count =$this->prepage;
				$curpage = ( int ) $this->_request->getParam ( 'page' );
				
				if (! $curpage)
						$curpage = 1;
					$offset = ($curpage - 1) * $count; 

				$sql="select SQL_CALC_FOUND_ROWS * from credit_goods where isdown=0 ORDER BY addtime desc limit $offset,$count ";
				//echo $sql;exit;
				$result=$this->_dbAdapter ->fetchAll ( $sql );
				foreach($result as $key=>$value){
					$result[$key]['goodsname']=str_replace("\n",'<br />',$value['goodsname']);
					$result[$key]['goodsname']=stripslashes($value['goodsname']);
					
					$result[$key]['intro']=str_replace("\n",'<br />',$value['intro']);
					$result[$key]['intro']=stripslashes($value['intro']);
				}
				$this->view->result=$result;
				//print_r($result);exit;
				$param ['file'] = '/' . $this->controller . '/' . $this->action;
				$param ['totalnum'] = $this->_dbAdapter ->fetchOne ( 'SELECT FOUND_ROWS()' ); 
				$param ['perpagenum'] = $count; // 每页显示的数目
				$param ['disnum'] = 11; // 取单数显示，当前页停在中间
				$param ['curpage'] = $curpage; // 当前页码
				$this->view->allnum=$param ['totalnum'];
				$this->view->pagenum=$param ['curpage'];
				
				$page = new MyClass_Page ( $param );
				//$page->setvar ( array ('groupid' => $groupid,'schoolid' => $schoolid) );
				$this->view->page= $page->getNumPage (); 
			
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	public function downAction(){
		try {
				$count =$this->prepage;
				$curpage = ( int ) $this->_request->getParam ( 'page' );
				
				if (! $curpage)
						$curpage = 1;
					$offset = ($curpage - 1) * $count; 

				$sql="select SQL_CALC_FOUND_ROWS * from credit_goods where isdown=1 ORDER BY addtime desc limit $offset,$count ";
				//echo $sql;exit;
				$result=$this->_dbAdapter ->fetchAll ( $sql );
				foreach($result as $key=>$value){
					$result[$key]['goodsname']=str_replace("\n",'<br />',$value['goodsname']);
					$result[$key]['goodsname']=stripslashes($value['goodsname']);
					
					$result[$key]['intro']=str_replace("\n",'<br />',$value['intro']);
					$result[$key]['intro']=stripslashes($value['intro']);
				}
				$this->view->result=$result;
				//print_r($result);exit;
				$param ['file'] = '/' . $this->controller . '/' . $this->action;
				$param ['totalnum'] = $this->_dbAdapter ->fetchOne ( 'SELECT FOUND_ROWS()' ); 
				$param ['perpagenum'] = $count; // 每页显示的数目
				$param ['disnum'] = 11; // 取单数显示，当前页停在中间
				$param ['curpage'] = $curpage; // 当前页码
				$this->view->allnum=$param ['totalnum'];
				$this->view->pagenum=$param ['curpage'];
				
				$page = new MyClass_Page ( $param );
				//$page->setvar ( array ('groupid' => $groupid,'schoolid' => $schoolid) );
				$this->view->page= $page->getNumPage (); 
			
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	
	public function deleteAction(){
		try {
			$goodsid = ( int ) $this->_request->getParam ( 'gid' );
			$page = ( int ) $this->_request->getParam ( 'page' );
	     	if (! $goodsid)
				throw new Exception ( '参数错误！' );
			
			$sql="update `credit_goods` set isdown=1 where goodsid='".$goodsid."'";
			$result=$this->_dbAdapter->query($sql);
			
			if ($result) {
				$this->feedback ( 'success', 'success', '/'.$this->controller.'/published/page/'.$page , 'tip', 3 );
			} else {
				throw new Exception ( '操作失败,或已经删除，请先刷新查看！' );//操作失败,或已经删除，请先刷新查看！
			}			
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}

	public function upAction(){
		try {
			$goodsid = ( int ) $this->_request->getParam ( 'gid' );
			$page = ( int ) $this->_request->getParam ( 'page' );
	     	if (! $goodsid)
				throw new Exception ( '参数错误！' );
			
			$sql="update `credit_goods` set isdown=0 where goodsid='".$goodsid."'";
			$result=$this->_dbAdapter->query($sql);
			
			if ($result) {
				$this->feedback ( 'success', 'success', '/'.$this->controller.'/down/page/'.$page , 'tip', 3 );
			} else {
				throw new Exception ( '操作失败,或已经删除，请先刷新查看！' );//操作失败,或已经删除，请先刷新查看！
			}			
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}

/*
物理删除
*/

	public function deleteabsAction(){
		try {
			$goodsid = ( int ) $this->_request->getParam ( 'gid' );
			$page = ( int ) $this->_request->getParam ( 'page' );
	     	if (! $goodsid)
				throw new Exception ( '参数错误！' );
			
			$sql="delete from `credit_goods` where goodsid='".$goodsid."'";
			$result=$this->_dbAdapter->query($sql);
			
			if ($result) {
				$this->feedback ( 'success', 'success', '/'.$this->controller.'/down/page/'.$page , 'tip', 3 );
			} else {
				throw new Exception ( '操作失败,或已经删除，请先刷新查看！' );//操作失败,或已经删除，请先刷新查看！
			}			
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}

	public function editAction(){
		try {
			$goodsid = ( int ) $this->_request->getParam ( 'gid' );
	     	if (! $goodsid)
				throw new Exception ( '参数错误！' );
			$sql="SELECT * FROM `credit_goods` where goodsid='".$goodsid."'";
			$result=$this->_dbAdapter ->fetchAll ( $sql );
			foreach($result as $key=>$value){
				$result[$key]['goodsname']=str_replace("\n",'<br />',$value['goodsname']);
				$result[$key]['goodsname']=stripslashes($value['goodsname']);
				
				$result[$key]['intro']=str_replace("\n",'<br />',$value['intro']);
				$result[$key]['intro']=stripslashes($value['intro']);
			}
			//print_r($result);exit;
			$this->view->result=$result;
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}

	public function editdownAction(){
		try {
			$goodsid = ( int ) $this->_request->getParam ( 'gid' );
	     	if (! $goodsid)
				throw new Exception ( '参数错误！' );
			$sql="SELECT * FROM `credit_goods` where goodsid='".$goodsid."'";
			$result=$this->_dbAdapter ->fetchAll ( $sql );
			foreach($result as $key=>$value){
				$result[$key]['goodsname']=str_replace("\n",'<br />',$value['goodsname']);
				$result[$key]['goodsname']=stripslashes($value['goodsname']);
				
				$result[$key]['intro']=str_replace("\n",'<br />',$value['intro']);
				$result[$key]['intro']=stripslashes($value['intro']);
			}
			//print_r($result);exit;
			$this->view->result=$result;
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}

	public function opeditdownAction(){
		try {
			if (! $this->isPost ()) {
				throw new Exception ( 'parameter_error' );
			}
			$p =$this->_request->getPost ();
			//print_r($p);exit;
			if($p['name']==''){
				throw new Exception ( '姓名不能为空' );
			}
			if($p['content']=='' ){
				throw new Exception ( '内容不能为空' );
			}
			if($p['score']=='' ){
				throw new Exception ( '内容不能为空' );
			}

			if($p['score']<0 ){
				throw new Exception ( '积分不能小于0' );
			}

			$p['score']=(int) $p['score'];
			$p['name']=strip_tags($p['name']);	
			$p['name']=addslashes($p['name']);
			
			$p['content']=strip_tags($p['content']);	
			$p['content']=addslashes($p['content']);
			
			
			$data=array(
				'goodsname'=>$p['name'],
				'credit'=>$p['score'],
				'addtime'=>time(),
				'intro'=>$p['content'],
			);
			
		    if(isset($_FILES['pic_url']['tmp_name']) && !empty($_FILES['pic_url']['tmp_name'])){
				
				
				  $file_path=PROJECT_ROOT .'/public/credit/';	
				  if (!file_exists($file_path)) {
					  mkdir($file_path);
				  }  
				  
				   $this->ThumbRatio($_FILES['pic_url']['tmp_name'],$file_path.$p['gid'].'.jpg.small.jpg',100,100,4);
				   $this->ThumbRatio($_FILES['pic_url']['tmp_name'],$file_path.$p['gid'].'.jpg',1136,640,1);
					  					
					$res1=Azure_Storage::defaultImg('credit',$file_path.$p['gid'].'.jpg.small.jpg',$p['gid'].'.jpg.small.jpg');
					$res2=Azure_Storage::defaultImg('credit',$file_path.$p['gid'].'.jpg',$p['gid'].'.jpg');
					
				  if($res1 && $res2){
					//删除本地图片
				//	unlink($file_path.$p['gid'].'.small.jpg');
				//	unlink($file_path.$p['gid'].'.jpg');
				  }
				  
				  $data['filename']=$p['gid'];
				  $data['filesize']=$_FILES['pic_url']['size'];
				  $data['fext']='jpg';


	   		} 
			$result = $this->_dbAdapter->update ( 'credit_goods', $data ,"goodsid='".$p['gid']."'" );
			
			if ($result) {
				$this->feedback ( '编辑成功', 'success', '/'.$this->controller.'/editdown/gid/'.$p['gid'] , 'tip', 3 );
			} else {
				throw new Exception ( '操作失败,或已经删除，请先刷新查看！' );//操作失败,或已经删除，请先刷新查看！
			}

		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	

	 //以下内容保持不变
	public function noRouteAction() {
		$this->_redirect ( '/' );
	}
	
	public function __call($method, $args) {
		if ('Action' == substr ( $method, - 6 )) {
			$url = '/' . $this->controller . '/index';
			return $this->_redirect ( $url );
		}
		throw new Exception ( 'Invalid method' );
	}
}

